//
//  SpecialCollectionViewController.swift
//  OverseasOnlineShopping
//
//  Created by 唐先才 on 16/3/9.
//  Copyright © 2016年 唐先才. All rights reserved.
//

import UIKit

class SpecialCollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {
    
    var collectionView: UICollectionView?
    var dataArray: NSMutableArray = []
    var specialID: NSString?
    var tmpDataDict: NSDictionary?
    var tmpArray: NSMutableArray = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        createData()
        createCollectionView()
    }
    
    func createData() {
        let manager: AFHTTPRequestOperationManager = AFHTTPRequestOperationManager()
        guard let specialID = self.specialID as? AnyObject else {
            return
        }
        let parameters = ["p_iSpecialID": specialID]
        manager.POST(specialCollection_url, parameters: parameters, success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) -> Void in
            guard let dataDict = responseObject as? [String: AnyObject] else {
                print("接受回来字典失败")
                return
            }
            self.tmpDataDict = dataDict
            guard let array = dataDict["listRecommendInfo"] as? [[String: AnyObject]] else {
                print("取出字典里对应的数组失败")
                return
            }
            for dict in array {
                let listModel = ListRecommendInfoModel(dict: dict)
                self.dataArray.addObject(listModel)
            }
            guard let collectionView = self.collectionView else {
                print("collectionView失败")
                return
            }
            collectionView.reloadData()
            }) { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
                
        }
    }
    
    func createCollectionView() {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .Vertical
        collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: WIDTH, height: HEIGHT - 80), collectionViewLayout: layout)
        guard let collectionView = self.collectionView else {
            return
        }
        collectionView.backgroundColor = BACKGROUD_COLOR
        self.view.addSubview(collectionView)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.showsVerticalScrollIndicator = false
        
        let nib = UINib(nibName: "SpecialCollectionViewCell", bundle: NSBundle.mainBundle())
        collectionView.registerNib(nib, forCellWithReuseIdentifier: "specialCollectionViewCell")
        collectionView.registerClass(SpecialHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "specialHeaderView")
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        return SpecialCollectionViewCell.configCellWithCollectionView(collectionView, indexPath: indexPath, dataArray: self.dataArray)
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSizeMake((WIDTH - 20) / 2, 320)
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(10, 5, 10, 5);
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
    }
    
    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
        guard let header = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "specialHeaderView", forIndexPath: indexPath) as? SpecialHeaderView else {
            return UICollectionReusableView()
        }
        guard let dict = self.tmpDataDict as? [String: AnyObject] else {
            print("头视图加载失败")
            return UICollectionReusableView()
        }
        let tmpModel = TxcModel(dict: dict)
        self.tmpArray.addObject(tmpModel)
        header.model = self.tmpArray[indexPath.row] as? TxcModel
        header.backgroundColor = BACKGROUD_COLOR
        return header
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        guard let dict = self.tmpDataDict as? [String: AnyObject] else {
//            print("头视图尺寸加载失败")
            return CGSizeZero
        }
        let tmpModel = TxcModel(dict: dict)
        let height = NSString(string: tmpModel.strSpecialContent).boundingRectWithSize(CGSizeMake(UIScreen.mainScreen().bounds.size.width - 40, 0), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(15)], context: nil).size.height
        return CGSizeMake(WIDTH, height + 10 + HEIGHT / 3 + 10)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
